<?php

class Technical_RoleController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
        $roles = new Application_Model_Dao_Role();
        $this->view->roles = $roles->getAll()->toArray();
    }

    public function addAction(){
    	
    	$form = new Application_Form_Role();
    	$form->appendValidationEngineFiles($this->view); 
    	$polish = BH_TranslatePL::getPolishTranslation();
  		$translate = new Zend_Translate('array', $polish, 'pl');
  		$form->setTranslator($translate);
    	
    	$form->submit->setLabel('Dodaj nową rolę');
		$this->view->RoleForm = $form;
   	
    	$request = $this->getRequest();
    	if ($request->isPost()) {
    		if ($form->isValid($request->getPost())) {
    			$role = new Application_Model_Entities_Role();
    			$role->setDesignation( $form->getValue('designation'));
    			$roleDao = new Application_Model_Dao_Role();
    			$roleDao->saveRole($role);
    		
    			$this->_helper->redirector('index');		
    		}
    	}
    }
  	public function editAction(){
		 
		$form = new Application_Form_Role();
		$form->appendValidationEngineFiles($this->view);
		$polish = BH_TranslatePL::getPolishTranslation();
		$translate = new Zend_Translate('array', $polish, 'pl');
		$form->setTranslator($translate);
		 
		$form->submit->setLabel('Edytuj role');
		$this->view->RoleEditForm = $form;

		if ($this->getRequest()->isPost()) {
			$formData = $this->getRequest()->getPost();
			if ($form->isValid($formData)) {
				$role = new Application_Model_Entities_Role();
				$role->setId( $form->getValue('id'));
				$role->setDesignation( $form->getValue('designation'));
				$roleDao = new Application_Model_Dao_Role();
				$roleDao->updateRole($role);

				$this->_helper->redirector('index');
			} else {
				$form->populate($formData);
			}
		}else{
			$id = $this->_getParam('id', 0);
			if ($id > 0) {
				$roleDao = new Application_Model_Dao_Role();
				$form->populate($roleDao->getRoleByID($id));
			}
		}
	}
    
    
}
